<template>
  <section class="todoapp">
    <TodoHeader @add="addFn" :arr="list"></TodoHeader>
    <TodoMain :arr="showList" @del="delFn"></TodoMain>
    <TodoFooter :arr="list" @finsh="finshFn" @changeType="changeTypeFn"></TodoFooter>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

import "./styles/base.css";
import "./styles/index.css";
export default {
  data() {
    return {
        type:'all',
      list: JSON.parse(localStorage.getItem('Todos')) || [],
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    addFn(name){
        this.list.push({
            id:+new Date(),
            name,
            isDone:false
        })
    },
    finshFn(){
      this.list=  this.list.filter(item=>!item.isDone)
    },
    changeTypeFn(val){
        this.type=val
    }
  },
  computed:{
    showList(){
        if(this.type==='active'){
            return this.list.filter(item=>!item.isDone)
        }else if(this.type==='finshed'){
            return this.list.filter(item=>item.isDone)
        }else{
            return this.list
        }
    }
  },
  watch:{
    list:{
        deep:true,
        handler(){
            localStorage.setItem('Todos',JSON.stringify(this.list))
        }
    }
  }
};
</script>
